Skip to content

fix(calibrate): refuse a payload whose shape is not one this code can read - #764

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-calibrate-payload-shape
Oct 1, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-calibrate-payload-shape

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

apply_calibration_payload is the applier behind Agent.load_calibration, ONNXAgent.load_calibration and the calibration= constructor argument — every one of them reading a JSON file from disk, where a hand edit is one keystroke away from the shape checks in the code. The function's own message says what it wants ("calibration JSON must contain temperature: [3 floats]"), but the check only looked at len() and payload.get():

version = payload.get("version", 1)
temps = payload.get("temperature")
if temps is None or len(temps) != N_QTYPES:        # len() of a dict or a str is not the shape
    raise ValueError("calibration JSON must contain temperature: [3 floats]")
_install_temperatures(obj, temps, payload.get("temperature_by_options") or {})  # dict() of anything

JSON hands back int, str, list and dict for the mistakes just as happily as the right shapes. Measured on main, each payload below does what the right-hand column says:

payload on main
["temperature", [1.0, 1.0, 1.0]] (whole file is a list) AttributeError: 'list' object has no attribute 'get'
{"temperature": 5} TypeError: object of type 'int' has no len()
{"temperature": "abc"} accepted — list("abc") installs its characters as temperatures
{"temperature": {"a": 1, "b": 2, "c": 3}} accepted — len 3 passes, the dict's keys are installed
{"temperature": [1.0] * 3, "version": "x"} ValueError: invalid literal for int() — no field named
{"temperature": [1.0] * 3, "version": true} accepted — int(True) is 1
{"temperature": [1.0] * 3, "version": {"v": 2}} TypeError: int() argument must be ... not 'dict'
{"temperature": [1.0] * 3, "temperature_by_options": 5} TypeError: 'int' object is not iterable
{"temperature": [1.0] * 3, "temperature_by_options": [["choice:2", 1.5]]} accepted — dict() converts the pair list

The accepted cases are the bad ones: a payload whose shape says nothing about temperatures loads with all-1.0 temperatures plus a warning that points at "invalid values" instead of "your file is the wrong shape", and the caller has no way to tell a loaded map from a refused one.

Fix

The shape is asserted before the tolerance that forgives values (which is unchanged — a non-numeric or out-of-range entry still clamps with the existing warning, and that is deliberate per the docstring):

  • payload must be an object
  • version must be an integer; a numeric string ("1") still parses exactly as before, true/{} are refused by name
  • temperature must be a list (or tuple) of N_QTYPES, so "abc" and a 3-key dict no longer pass a length check that never asked their type
  • temperature_by_options must be an object; a pair list is no longer silently converted

Every refusal is the ValueError the function already promises, naming the field.

Conflict check

laya/calibrate.py is untouched by all 67 open PRs. tests/test_calibrate.py is touched only by my own open #731 (docstring pins), whose hunks sit at the import line and the constructor-wiring section — the new block sits ~11 lines above the nearest of them, non-overlapping.

Verification

python tests/test_calibrate.py            132 passed, 0 failed   (baseline on main: 121)
python tests/test_calibration_persistence.py  OK
python tests/test_temperature_loading.py      OK
python tests/test_load_errors.py           23 passed, 0 failed
ruff + compileall: clean

Reverting only laya/calibrate.py fails the nine new refusal checks — with the exact AttributeError/TypeErrors and silent accepts in the table above — so they are real regression guards; the tenth new check pins that a numeric-string version still loads, and the wrong-length refusal (already caught before this change) stays as a pin.

@NandhaKishorM
NandhaKishorM merged commit d8d0b76 into NandhaKishorM:main Oct 1, 2026
24 of 25 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged for 0.3.23.

Verified on the integrated branch, not on the PR in isolation: every suite in the ci.yml test lane plus the pytest lane green, compileall clean, and a decision diff against main on the real english and multilingual checkpoints (72 answers over choice, score and noul, short and long states, budgeted and predict_long) showing zero decision flips and a max probability delta of 0.000000.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants